﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

#endregion


namespace ScrumTable.UI.View.Converter
{

    /// <summary>
    /// Converter to return Visibile if the defined State in the parameter matches the current state.
    /// </summary>
    [ValueConversion(typeof(Enum), typeof(object))]
    public class ConverterEnumEqual : IValueConverter
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Checks if the enum value is in the parameter. Parameter can be splitted with ",". If true then Visibility.Visible else Visibility.Collapsed
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// If value (enum value) is in parameter then Visibility.Visible else Visibility.Collapsed
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var en1 = value;
            var en2 = parameter;
            var en1Type = en1.GetType();
            var en2Type = en2.GetType();

            if(en2Type.Equals(typeof(string)))
            {
                if(((string)en2).Contains(" "))
                    throw new ArgumentException("No spaces! Seperate only with ','.");
                var values = ((string) en2).Split(',');
                foreach (var v in values)
                {
                    var valueName = Enum.GetName(en1Type, en1);
                    if(valueName.Equals(v))
                        return GetRealReturnValue(true, targetType);
                }
                return GetRealReturnValue(false, targetType);
            }

            if (!en1Type.Equals(en2Type)) return GetRealReturnValue(false, targetType);

            return GetRealReturnValue(en1.Equals(en2), targetType);
        }

        /// <summary>
        /// Not supported. Throws NotSupportedException.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();

        }

        private static object GetRealReturnValue(bool value, Type targetType)
        {
            if (targetType.Equals(typeof(bool)))
            {
                return value;
            }

            if(targetType.Equals(typeof(Visibility)))
            {
                return (value) ? Visibility.Visible : Visibility.Collapsed;
            }

            return false;
        }
       
        #endregion
       
    }
}
